import copy

import wx


class DiaTable5Add(wx.Dialog):
    Names = ['费用名称', '计算依据及方法', '价格(除税)', '税率']
    Widths = [(120, -1), (300, -1), (50, -1), (60, -1), (60, -1),]
    BtnNames = ['保存', '取消']
    Choices = ['服务费税率', '施工费税率', '材料费税率', '自定义税率']
    Fun = ['self.OnSave', 'self.OnCancel', ]
    def __init__(self, parent, title, *args, **kwargs):
        self.taxs = kwargs.get('taxs', ['0', '0', '0'])
        self.datas = kwargs.get('datas', ['', '', '', ''])
        super().__init__(parent, -1, title)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        btnsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.inputsizer = wx.BoxSizer(wx.HORIZONTAL)
        # 初始化第一排
        self.InitTextCtrl()
        # 初始化btn
        self.InitBtn(btnsizer)

        mainsizer.Add(self.inputsizer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        mainsizer.Add(btnsizer, 0, flag=wx.ALL|wx.ALIGN_RIGHT, border=5)

        self.SetSizer(mainsizer)
        mainsizer.Fit(self)
        mainsizer.SetSizeHints(self)
    
    def InitTextCtrl(self):
        for index, name in enumerate(self.Names):
            thissizer = wx.BoxSizer(wx.VERTICAL)
            statictext = wx.StaticText(self, -1, label=name, name=name+'changestatictext')
            textctrl = wx.TextCtrl(self, -1, size=self.Widths[index], name=name+'changetextctrl', value=str(self.datas[index]))
            thissizer.Add(statictext, 0, flag=wx.ALL|wx.EXPAND, border=5)
            thissizer.Add(textctrl, 0, flag=wx.ALL|wx.EXPAND, border=5)
            self.inputsizer.Add(thissizer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        thissizer = wx.BoxSizer(wx.VERTICAL)
        # 
        choicer = wx.Choice(self, -1, choices=self.Choices, name='changechoicer')
        name = '增值税类型'
        statictext = wx.StaticText(self, -1, label=name, name=name+'changestatictext')
        thissizer.Add(statictext, 0, flag=wx.ALL|wx.EXPAND, border=5)
        thissizer.Add(choicer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        self.inputsizer.Insert(3, thissizer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        self.Bind(wx.EVT_CHOICE, self.OnChoice, choicer)
        choicer.SetSelection(3)

    def InitBtn(self, sizer):
        for index, name in enumerate(self.BtnNames):
            btn = wx.Button(self, -1, label=name, name=name+'btn')
            self.Bind(wx.EVT_BUTTON, eval(self.Fun[index]), btn)
            # self.Bind(wx.EVT_BUTTON, self.OnCopy, btn)
            sizer.Add(btn, 0, flag=wx.ALL|wx.EXPAND, border=5)

    def OnSave(self, event):
        waringemptnames = ['费用名称', '价格(除税)', '税率']
        waringnumnames = ['价格(除税)', '税率']
        waringemptmessage = '数据不能为空！'
        waringnummessage = '填入数据必须为数字！'
        waringzeronames = ['价格(除税)']
        waringzeromessage = '填入数字不能为0！'

        for index, name in enumerate(self.Names):
            obj = self.FindWindowByName(name+'changetextctrl')
            text = obj.GetLineText(0)
            if name in waringemptnames and text == '':
                wx.MessageDialog(self, '"' + name + '"' + waringemptmessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING).ShowModal()
                return
            if name in waringnumnames and not self.isnum(text):
                wx.MessageDialog(self, '"' + name + '"' + waringnummessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING).ShowModal()
                return
            if name in waringzeronames and float(text) == 0:
                wx.MessageDialog(self, '"' + name + '"' + waringzeromessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING).ShowModal()
                return
            self.datas[index] = text

        self.EndModal(retCode=1)
    
    def OnChoice(self, event):
        index = event.GetSelection()
        string = event.GetString()
        textctrl = self.FindWindowByName(self.Names[3]+'changetextctrl')
        if string == self.Choices[3]:
            textctrl.SetEditable(True)
        else:
            textctrl.SetEditable(False)
            textctrl.Clear()
            textctrl.AppendText(str(self.taxs[index]))

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

    def ShowModal(self):
        ret = super().ShowModal()
        if ret == 5101:
            return ret
        elif ret == 1:
            return self.datas
        else:
            return 5101

    def isnum(self, string):
        try:
            if string=='NaN':
                return False
            float(string)
            return True
        except ValueError:
            return False

class DiaTable5ChangeRate(wx.Dialog):
    Names = ['勘察费费率', '设计费费率', '监理费费率', '安全生产费费率', '审计费费率', '设计阶段系数']
    Widths = [(120, -1), (120, -1), (120, -1), (120, -1), (120, -1), (120, -1),]
    BtnNames = ['保存', '取消']
    Fun = ['self.OnSave', 'self.OnCancel', ]
    def __init__(self, parent, title, *args, **kwargs):
        self.datas = kwargs.get('datas', ['', '', '', '', ''])
        super().__init__(parent, -1, title)
        mainsizer = wx.BoxSizer(wx.VERTICAL)
        btnsizer = wx.BoxSizer(wx.HORIZONTAL)
        self.inputsizer = wx.BoxSizer(wx.HORIZONTAL)
        # 初始化第一排
        self.InitTextCtrl()
        # 初始化btn
        self.InitBtn(btnsizer)

        mainsizer.Add(self.inputsizer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        mainsizer.Add(btnsizer, 0, flag=wx.ALL|wx.ALIGN_RIGHT, border=5)

        self.SetSizer(mainsizer)
        mainsizer.Fit(self)
        mainsizer.SetSizeHints(self)
    
    def InitTextCtrl(self):
        for index, name in enumerate(self.Names):
            thissizer = wx.BoxSizer(wx.VERTICAL)
            statictext = wx.StaticText(self, -1, label=name, name=name+'changestatictext')
            textctrl = wx.TextCtrl(self, -1, size=self.Widths[index], name=name+'changetextctrl', value=str(self.datas[index]))
            thissizer.Add(statictext, 0, flag=wx.ALL|wx.EXPAND, border=5)
            thissizer.Add(textctrl, 0, flag=wx.ALL|wx.EXPAND, border=5)
            self.inputsizer.Add(thissizer, 0, flag=wx.ALL|wx.EXPAND, border=5)
        thissizer = wx.BoxSizer(wx.VERTICAL)
        # 

    def InitBtn(self, sizer):
        for index, name in enumerate(self.BtnNames):
            btn = wx.Button(self, -1, label=name, name=name+'btn')
            self.Bind(wx.EVT_BUTTON, eval(self.Fun[index]), btn)
            # self.Bind(wx.EVT_BUTTON, self.OnCopy, btn)
            sizer.Add(btn, 0, flag=wx.ALL|wx.EXPAND, border=5)

    def OnSave(self, event):
        waringemptnames = ['勘察费费率', '设计费费率', '监理费费率', '安全生产费费率']
        waringnumnames = ['勘察费费率', '设计费费率', '监理费费率', '安全生产费费率']
        waringemptmessage = '数据不能为空！'
        waringnummessage = '填入数据必须为数字或公式！'

        for index, name in enumerate(self.Names):
            obj = self.FindWindowByName(name+'changetextctrl')
            text = obj.GetLineText(0)
            if name in waringemptnames and text == '':
                dia = wx.MessageDialog(self, '"' + name + '"' + waringemptmessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING)
                dia.ShowModal()
                dia.Destroy()
                return
            if name in waringnumnames and not self.isformula(text):
                dia = wx.MessageDialog(self, '"' + name + '"' + waringnummessage,
                    caption='警告',style=wx.OK|wx.ICON_WARNING)
                dia.ShowModal()
                dia.Destroy()
                return
            self.datas[index] = text

        self.EndModal(retCode=1)
    
    def OnChoice(self, event):
        index = event.GetSelection()
        string = event.GetString()
        textctrl = self.FindWindowByName(self.Names[3]+'changetextctrl')
        if string == self.Choices[3]:
            textctrl.SetEditable(True)
        else:
            textctrl.SetEditable(False)
            textctrl.Clear()
            textctrl.AppendText(self.taxs[index])

    def OnCancel(self, event):
        self.EndModal(retCode=5101)

    def ShowModal(self):
        ret = super().ShowModal()
        if ret == 5101:
            return ret
        elif ret == 1:
            return self.datas
        else:
            return 5101

    def isnum(self, string):
        try:
            if string=='NaN':
                return False
            float(string)
            return True
        except ValueError:
            return False

    def isformula(self, string):
        try:
            eval(string)
            return True
        except NameError:
            return False
        except SyntaxError:
            return False

if __name__ == "__main__":
    import os
    class MyFrame(wx.Frame):
        def __init__(self):
            super().__init__(None, -1, 'haha', size=(200, 100))
            btn = wx.Button(self, -1, 'baba', size=(10, 10))
            self.Bind(wx.EVT_BUTTON, self.OnClick, btn)

        def OnClick(self, event):
            data = ['测试1', '测试2', '测试3', '测试4', '电缆']
            dia = DiaTable5Add(self, 'haha', data=data, name='haha')
            modal = dia.ShowModal()
            print(modal)
            dia.Destroy()

    app = wx.App()
    MyFrame().Show()
    app.MainLoop()
